
package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.LmbCouPurPlanDAO;
import com.trkj.dao.jpa.LmbCousPurPlandeDAO;
import com.trkj.dao.jpa.LmbCousumableDAO;
import com.trkj.dao.jpa.LmbSupplierDAO;
import com.trkj.dao.jpa.LmbTbPostDAO;
import com.trkj.dao.jpa.LmbTbstaffDAO;
import com.trkj.dao.jpa.TbdepartmentDAO;
import com.trkj.pojos.LmbConsumable;
import com.trkj.pojos.LmbCousumablePlanDetail;
import com.trkj.pojos.LmbCousumablePurchasePlan;
import com.trkj.pojos.Tbdepartment;
import com.trkj.pojos.Tbpost;
import com.trkj.pojos.Tbstaff;
import com.trkj.pojos.YzrSupplier;
import com.trkj.vo.LmbCurPlanVo;
import com.trkj.vo.LmbCurPurPlanDetailsVo;
import com.trkj.vo.LmbCurPurPlanVo;
import com.trkj.vo.LmbStaffVo;

@Service
@Transactional
public class LmbOddNumbersService {
	@Autowired
	private LmbCouPurPlanDAO couPurPlanDAO;
	@Autowired
	private LmbCousPurPlandeDAO cousPurPlandeDAO;
	@Autowired
	private LmbTbPostDAO LmbtbpostDAO;
	@Autowired
	private TbdepartmentDAO lmbdepartmentDAO;
	@Autowired
	private LmbTbstaffDAO lmbtbstaffDAO;
	@Autowired
	private LmbSupplierDAO lmbsupplierDAO;
	@Autowired
	private LmbCousumableDAO lmbconsumableDAO;
	
	/**
	 * 生成各种表的编号
	 * @param name
	 * @return
	 */
	public String getId(String name,String n){					
		if(n==null){
			n=name+"000001";
		}else{
			long x=Integer.parseInt(n.substring (name.length())) +1;
			if(x<10){
				n=name+"00000"+x;
			}else if(x>=10 && x<100){
				n=name+"0000"+x;
			}else if(x>=100 && x<1000){
				n=name+"000"+x;
			}else if(x>=1000 && x<10000){
				n=name+"00"+x;
			}else if(x>=10000 && x<100000){
				n=name+"0"+x;
			}else {
				n=name+x;
			}
		}
		System.out.println("生成的编号:"+n);
		return n;
	}
	
	public String rq(){
		String s=null;
		// 获取年份
		int year = new java.util.Date().getYear()+1900;
		//获取月份
		int month = new java.util.Date().getMonth()+1;
		//获取日期
		int date = new java.util.Date().getDate();
		if(month<10){
			if(date<10){
				s=year+"0"+month+"0"+date;
				System.out.println("s为"+s);
			}else{	
				s=year+"0"+month+date;
				System.out.println("s为"+s);
			}			
		}else{	
			if(date<10){
				s=year+month+"0"+date;
				System.out.println("s为"+s);
			}else{	
				s=year+""+month+date;
				System.out.println("s为"+s);
			}	
			
		}
		return s;
	}

	/**
	 * 将员工的po转换成Vo
	 * @param staff
	 * @return
	 */
	private LmbStaffVo staffPo2Vo(Tbstaff staff){
		LmbStaffVo staffVo = new LmbStaffVo();
		staffVo.setStaffNo(staff.getStaffNo());
		staffVo.setStaffName(staff.getStaffName());
		
		BigDecimal departId = staff.getDepartmentNo();
		if(departId !=null){
			Tbdepartment department = lmbdepartmentDAO.findOne(departId.longValue());
			staffVo.setDepartno(department.getDepartmentNo());
			staffVo.setDepartmentName(department.getDepartmentName());
		}else{
			staffVo.setDepartno(0);
			staffVo.setDepartmentName(null);
		}
		
		BigDecimal postno = staff.getStaffPostNo();	
		if(postno != null){
			long postNo = postno.longValue();
			Tbpost post = LmbtbpostDAO.findOne(postNo);
			staffVo.setPostno(post.getPostNo());
			staffVo.setPostname(post.getPostName());
		}else{
			staffVo.setPostno(0);
			staffVo.setPostname(null);
		}
		return staffVo;
	}
	
	/**
	 * 将员工的集合转换成Vo的集合
	 * @param list
	 * @return
	 */
	private List<LmbStaffVo> poStaffList2VoList(List<Tbstaff> list){
		if(list == null) return null;
		List<LmbStaffVo> listVo = new ArrayList<>();
		for(Tbstaff staff: list){
			LmbStaffVo staffvo = staffPo2Vo(staff);
			listVo.add(staffvo);
		}
		return listVo;
	}
	
	/**
	 * 查找员工
	 * @return
	 */
	public List<LmbStaffVo> findStaff(){
		List<Tbstaff> staffList =  (List<Tbstaff>) lmbtbstaffDAO.findAll();
		return poStaffList2VoList(staffList);
	}
	
	/**
	 * 查找供应商
	 * @return
	 */
	public List<YzrSupplier> findSupplier(){
		List<YzrSupplier> supplierList = (List<YzrSupplier>) lmbsupplierDAO.findAll();
		return supplierList;
	}
	
	/**
	 * 查找耗材
	 * @return
	 */
	public List<LmbConsumable> findConsumable(){
		List<LmbConsumable> consumableList=(List<LmbConsumable>) lmbconsumableDAO.findAll();
		return consumableList;
	}
	
	/**
	 * 添加采购计划及其详细
	 * @param curPlanVo
	 */
	public void doPlan(LmbCurPlanVo curPlanVo){
		//保存采购计划的基本信息
		// 获取日期
		String date = this.rq();
		//拼接
		String name = "HCJ-" + date;					
		String n = couPurPlanDAO.findMaxID(name);	
		System.out.println("从数据库中读取的最大采购计划编号："+n);
		String sid =this.getId(name, n);
		LmbCousumablePurchasePlan cp =  curPlanVo.getCoulPurlPlan();
		cp.setCpPlanId(sid);
		cp.setCpPlanStatus("待审批");
		cp.setCpPlanPurstatus("未采购");
		System.out.println(cp.getCpPlanId());
		couPurPlanDAO.save(cp);
		//保存采购计划明细
		List<LmbCousumablePlanDetail> list = curPlanVo.getCouPlanDetail();
		System.out.println("list的长度："+list.size());
		if(list != null){	
			//创建采购计划明细与采购计划的关系
			for(LmbCousumablePlanDetail detail:list){
				//拼接
				String name1 = "HCJX-" + date;	
				String n1 = cousPurPlandeDAO.findMaxID(name1);	
				String sid1 =this.getId(name1, n1);
				detail.setCpdId(sid1);
				detail.setCpdCpPlanId(sid);		
				detail.setCpdName(lmbconsumableDAO.findName(detail.getCpdCId()));
				cousPurPlandeDAO.save(detail);
			}					
		}					
	}
	
	/**
	 * 将LmbCousumablePurchasePlan转成LmbCurPurPlanVo
	 * @param curPurPlan
	 * @return
	 */
	private LmbCurPurPlanVo curPurPlanPo2Vo(LmbCousumablePurchasePlan curPurPlan){
		LmbCurPurPlanVo curPurPlanVo = new LmbCurPurPlanVo();
		curPurPlanVo.setCpPlanId(curPurPlan.getCpPlanId());
		curPurPlanVo.setCpPlanTitle(curPurPlan.getCpPlanTitle());
		curPurPlanVo.setCpPlanStatus(curPurPlan.getCpPlanStatus());
		curPurPlanVo.setCpPlanDate(curPurPlan.getCpPlanDate());
		curPurPlanVo.setCpPlanApprovedate(curPurPlan.getCpPlanApprovedate());
		curPurPlanVo.setCpPlanAppRemark(curPurPlan.getCpPlanAppRemark());
		long cpPlanMakepeopleNo = curPurPlan.getCpPlanMakepeople().longValue();
		Tbstaff staff = lmbtbstaffDAO.findOne(cpPlanMakepeopleNo);
		System.out.println("cpPlanMakepeopleNo为："+cpPlanMakepeopleNo);
		curPurPlanVo.setCpPlanMakepeople(curPurPlan.getCpPlanMakepeople());
		curPurPlanVo.setMname(staff.getStaffName());
		curPurPlanVo.setMno(staff.getStaffNo());
		long cpPlanApprovepeopleNo = curPurPlan.getCpPlanApprovepeople().longValue();
		Tbstaff staff1 = lmbtbstaffDAO.findOne(cpPlanApprovepeopleNo);
		curPurPlanVo.setCpPlanApprovepeople(curPurPlan.getCpPlanApprovepeople());
		curPurPlanVo.setPno(staff1.getStaffNo());
		curPurPlanVo.setPname(staff1.getStaffName());
		curPurPlanVo.setCpPlanAppresult(curPurPlan.getCpPlanAppresult());
		curPurPlanVo.setCpPlanPurstatus(curPurPlan.getCpPlanPurstatus());
		System.out.println("curPurPlanVo:"+curPurPlanVo.getCpPlanAppresult());
		System.out.println("curPurPlanVo:"+curPurPlanVo.getCpPlanPurstatus());
		return curPurPlanVo;
	}
	
	/**
	 * 将LmbCousumablePurchasePlan的集合转换成LmbCurPurPlanVo的List集合
	 * @param list
	 * @return
	 */
	private List<LmbCurPurPlanVo> poCouPurPlanList2VoList(List<LmbCousumablePurchasePlan> list){
		if(list == null) {
			System.out.println("为空");
			return null;
		}
		List<LmbCurPurPlanVo> listVo = new ArrayList<>();
		for(LmbCousumablePurchasePlan couPurPlan: list){
			LmbCurPurPlanVo curPurPlanVo = curPurPlanPo2Vo(couPurPlan);
			listVo.add(curPurPlanVo);
		}
		return listVo;
	}
	
	/**
	 * 将LmbCousumablePlanDetail转成LmbCurPurPlanDetailsVo
	 * @param curPurPlan
	 * @return
	 */
	private LmbCurPurPlanDetailsVo curPurPlanDetailsPo2Vo(LmbCousumablePlanDetail curPurPlandetails){
		LmbCurPurPlanDetailsVo curPurPlanDetailsVo = new LmbCurPurPlanDetailsVo();
		curPurPlanDetailsVo.setCpdId(curPurPlandetails.getCpdId());
		curPurPlanDetailsVo.setCpdCpPlanId(curPurPlandetails.getCpdCpPlanId());
		curPurPlanDetailsVo.setCpdMoney(curPurPlandetails.getCpdMoney());
		curPurPlanDetailsVo.setCpdNum(curPurPlandetails.getCpdNum());
		curPurPlanDetailsVo.setCpdCId(curPurPlandetails.getCpdCId());
		curPurPlanDetailsVo.setCpdName(curPurPlandetails.getCpdName());
		curPurPlanDetailsVo.setCpdNorms(curPurPlandetails.getCpdNorms());
		YzrSupplier supplier = lmbsupplierDAO.findOne(curPurPlandetails.getCpdSupplier());		
		curPurPlanDetailsVo.setCpdSupplier(supplier.getSupId());
		curPurPlanDetailsVo.setGno(supplier.getSupId());
		curPurPlanDetailsVo.setGname(supplier.getSupName());
		return curPurPlanDetailsVo;
	}
	
	/**
	 * 将LmbCousumablePlanDetail的集合转换成LmbCurPurPlanDetailsVo的List集合
	 * @param list
	 * @return
	 */
	private List<LmbCurPurPlanDetailsVo> poCouPurPlanDetailsList2VoList(List<LmbCousumablePlanDetail> list){
		if(list == null) return null;
		List<LmbCurPurPlanDetailsVo> listVo = new ArrayList<>();
		for(LmbCousumablePlanDetail couPlanDetail: list){
			LmbCurPurPlanDetailsVo curPurPlanDetailsVo = curPurPlanDetailsPo2Vo(couPlanDetail);
			listVo.add(curPurPlanDetailsVo);
		}
		return listVo;
	}
		
	
	/**
	 * 分页查询采购计划
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map<String,Object> queryCurPurPlanByPage(int page,int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		//获取当前页的记录
		List<LmbCousumablePurchasePlan> list = couPurPlanDAO.findCurPurPlanByPage(pr);
		//获取总的记录条数
		long total = couPurPlanDAO.count();
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows",poCouPurPlanList2VoList(list));
		return map;
	}
	
	/**
	 * 批量删除采购计划
	 * @param empnos
	 */
	public void deleteCurPurPlan(String[] list3){
		//将数组转换成集合
		DefaultConversionService service =
				new DefaultConversionService();
		List<String> list = service.convert(list3, List.class);
		for(String l: list){
			couPurPlanDAO.delete(l);
			List<LmbCousumablePlanDetail> list2 = cousPurPlandeDAO.findByCpdCpPlanId(l);
			cousPurPlandeDAO.delete(list2);
		}
	}
	
	
	/**
	 * 根据id查询采购计划
	 * @param page
	 * @param rows
	 * @return
	 */
	public LmbCurPurPlanVo queryCurPurPlan(String id){	
		LmbCousumablePurchasePlan couPurPlan = couPurPlanDAO.findOne(id);
		LmbCurPurPlanVo curPlanVo = curPurPlanPo2Vo(couPurPlan);		
		return curPlanVo;
	}
	
	/**
	 * 分页查询采购计划详情
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map<String,Object> queryCurPurPlanDetailsByPage(String id,int page,int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		//获取当前页的记录
		List<LmbCousumablePlanDetail> list = cousPurPlandeDAO.findCurPurPlanDetailsByPage(id, pr);
		//获取总的记录条数
		long total = cousPurPlandeDAO.count();
		System.out.println("total"+total);
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows",poCouPurPlanDetailsList2VoList(list));
		return map;
	}
	
	public Map<String,Object> queryCurPurPlanByStatus(String status,int page,int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		//获取当前页的记录
		List<LmbCousumablePurchasePlan> list = couPurPlanDAO.findCurPurPlanByStatus(pr, status);
		long total = couPurPlanDAO.findByCount(status);
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", poCouPurPlanList2VoList(list));
		return map;		
	}
	
	public Map<String,Object> queryCurPurPlanByTitle(String title,int page,int rows){
		PageRequest pr = new PageRequest(page - 1,rows);
		//获取当前页的记录
		List<LmbCousumablePurchasePlan> list = couPurPlanDAO.findCurPurPlanByTitle(pr,title);
		long total = couPurPlanDAO.findByTitleCount(title);
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", poCouPurPlanList2VoList(list));
		return map;		
	}
	
	public Map<String,Object> queryCurpurByPurstatus(String purstatus,int page,int rows){
		PageRequest pr = new PageRequest(page - 1,rows);
		//获取当前页的记录
		List<LmbCousumablePurchasePlan> list = couPurPlanDAO.findBycpPlanPurstatus(pr, purstatus);
		long total = couPurPlanDAO.findBycpPlanPurstatusCount(purstatus);
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", poCouPurPlanList2VoList(list));
		return map;
	}
	
	
	/**
	 * 修改采购计划
	 * @param cpPlanId
	 * @param cpPlanTitle
	 * @param cpPlanMakepeople
	 * @param cpPlanApprovepeople
	 * @param cpPlanDate
	 * @param curPlanVo
	 */
	public void editCouPurPlan(String cpPlanId,String cpPlanTitle,BigDecimal cpPlanMakepeople,BigDecimal cpPlanApprovepeople,Date cpPlanDate,LmbCurPlanVo curPlanVo){
		//根据计划编号找到之前的采购信息，依次覆盖
		LmbCousumablePurchasePlan couPurPlan = couPurPlanDAO.findOne(cpPlanId);
		couPurPlan.setCpPlanTitle(cpPlanTitle);
		couPurPlan.setCpPlanMakepeople(cpPlanMakepeople);
		couPurPlan.setCpPlanApprovepeople(cpPlanApprovepeople);
		couPurPlan.setCpPlanDate(cpPlanDate);
		couPurPlanDAO.save(couPurPlan);
		
		//找出数据库内该计划单号下的计划明细，删除
		List<LmbCousumablePlanDetail> srcCoupPurDet = cousPurPlandeDAO.findByCpdCpPlanId(cpPlanId);
		cousPurPlandeDAO.delete(srcCoupPurDet);
		
		//判断计划明细编号是否存在，如果存在就覆盖，不存在就生成一个编号，将信息存入
		List<LmbCousumablePlanDetail> list = curPlanVo.getCouPlanDetail();
		if(list != null){
			for(LmbCousumablePlanDetail couPurDet:list){
				System.out.println("输出："+couPurDet.getCpdId().length());
				if(couPurDet.getCpdId().length()>1){
					System.out.println("旧的");
					LmbCousumablePlanDetail temp = new LmbCousumablePlanDetail();
					temp.setCpdId(couPurDet.getCpdId());
					temp.setCpdCpPlanId(couPurDet.getCpdCpPlanId());
					temp.setCpdCId(couPurDet.getCpdCId());
					temp.setCpdName(couPurDet.getCpdName());
					temp.setCpdNorms(couPurDet.getCpdNorms());
					temp.setCpdNum(couPurDet.getCpdNum());
					temp.setCpdSupplier(couPurDet.getCpdSupplier());
					temp.setCpdMoney(couPurDet.getCpdMoney());
					cousPurPlandeDAO.save(couPurDet);
				}else{
					System.err.println("新的");
					//生成编号
					String date = rq();
					//拼接
					String nameDet = "HCJX-"+date;
					String n = cousPurPlandeDAO.findMaxID(nameDet);
					LmbCousumablePlanDetail temp = new LmbCousumablePlanDetail();
					String name = getId(nameDet,n);
					temp.setCpdId(name);
					temp.setCpdCpPlanId(cpPlanId);
					temp.setCpdCId(couPurDet.getCpdCId());
					temp.setCpdName(couPurDet.getCpdName());
					temp.setCpdNorms(couPurDet.getCpdNorms());
					temp.setCpdNum(couPurDet.getCpdNum());
					temp.setCpdSupplier(couPurDet.getCpdSupplier());
					temp.setCpdMoney(couPurDet.getCpdMoney());
					cousPurPlandeDAO.save(temp);}
			}
		}
	}
	
	/**
	 * 审批耗材采购计划
	 * @param id
	 * @param jg
	 * @param bz
	 */
	public void spCouPurPlan(String id,String jg,String bz){
		LmbCousumablePurchasePlan couPurPlan = couPurPlanDAO.findOne(id);
		couPurPlan.setCpPlanStatus("已审批");
		couPurPlan.setCpPlanApprovedate(new Date());
		couPurPlan.setCpPlanAppresult(jg);
		couPurPlan.setCpPlanAppRemark(bz);
		couPurPlanDAO.save(couPurPlan);
	}
	
	public Map<String,Object> queryByApprovepeople(BigDecimal spr,int page,int rows){
		PageRequest pr = new PageRequest(page - 1,rows);
		//获取当前页的记录
		List<LmbCousumablePurchasePlan> list = couPurPlanDAO.findByApprovepeople(pr,spr);
		long total = couPurPlanDAO.CountFindByApprovepeople(spr);
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", poCouPurPlanList2VoList(list));
		return map;
	}
	
	public Map<String,Object> queryByApprovepeopleYsp(BigDecimal spr,int page,int rows){
		PageRequest pr = new PageRequest(page - 1,rows);
		//获取当前页的记录
		List<LmbCousumablePurchasePlan> list = couPurPlanDAO.findPlanByspr(pr, spr);
		long total = couPurPlanDAO.countfindPlanByspr(spr);
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", poCouPurPlanList2VoList(list));
		return map;
	}
}
